package com.wondersgroup.fjzlpt.org.dao.impl.fwxx;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.quick.framework.boot.repository.sql.bean.SQLBean;
import com.quick.framework.boot.repository.sql.bean.page.PageQuerySQLBean;
import com.quick.framework.boot.repository.sql.bean.table.TableBean;
import com.quick.framework.boot.repository.sql.jdbcTemplate.impl.BaseJTDAOImpl;
import com.quick.framework.boot.repository.sql.jdbcTemplate.rs.ResultHandlerConstant;
import com.wondersgroup.fjzlpt.org.dao.inter.fwxx.FwxxDAO;
@Repository
public class FwxxDAOImpl extends BaseJTDAOImpl implements FwxxDAO{
	@Override
	@Autowired
	@Qualifier("TB_XXWH_FWXX")
	public void setTb(TableBean tb) {
		super.setTb(tb);
	}
	
	@Value("${common.WSGLJGLBDM}")
	private String jglb;
	/**
	 * 分页查询
	 */
	public JSONObject queryPage(Map<String, String> map) {
		JSONObject sqlObj = this.getPageSql(map);
		String sql = sqlObj.getString("sql");
		JSONArray sqlParams = sqlObj.getJSONArray("sqlParams");
		
		PageQuerySQLBean sb = super.checkAndCreatePageQuerySQLBean(map, sql, "order by a.CJSJ DESC , a.fwid ", sqlParams.toArray());
		return super.queryPage(sb, ResultHandlerConstant.JSONARRAY);
	}
	
	/**
	 * 根据条件查询所有数据
	 * @param map
	 * @return
	 */
	public JSONArray queryAll(Map<String, String> map){
		JSONObject sqlObj = this.getPageSql(map);
		String sql = sqlObj.getString("sql") + " order by a.CJSJ DESC";
		JSONArray sqlParams = sqlObj.getJSONArray("sqlParams");
		
		SQLBean bean = new SQLBean(sql, sqlParams.toArray());
		return super.queryList(bean);
	}
	
	/**
	 * 合成基本的分页sql
	 * @param map
	 * @return
	 */
	private JSONObject getPageSql(Map<String, String> map){
		JSONObject result = new JSONObject();
		
		StringBuilder sbud = new StringBuilder("select a.*,b.YLJGMC,");
		sbud.append("(select XMMC from TB_ZY_SERVICE_TYPE where XMDM = a.FWLB) FWLBMC,");
		sbud.append("(select XMMC from TB_ZY_ZT where XMDM = a.FWZT) FWZTMC,");
		sbud.append("(select XMMC from TB_ZY_QY_TYPE where XMDM = a.QYLB) QYLBMC  ");
		sbud.append(" from TB_XXWH_FWXX a, TB_ZD_YLJGXX b \n");
		sbud.append(" where a.YLJGID = b.PTJGID  \n");
		List list = new ArrayList();
		String value = map.get("FWMC");
		if(!StringUtils.isEmpty(value))
		{
			sbud.append("and a.FWMC like ? ");
			list.add("%"+value+"%");
		}
		/*value = map.get("FWNR");
		if(!StringUtils.isEmpty(value))
		{
			sbud.append("and a.FWNR like ? ");
			list.add("%"+value+"%");
		}*/
		//服务类别(等于)
		value = map.get("FWLB");
		if(!StringUtils.isEmpty(value))
		{
			sbud.append("and a.FWLB = ? ");
			list.add(value);
		}
		//服务类别(不等于)
		value = map.get("not_FWLB");
		if(!StringUtils.isEmpty(value))
		{
			sbud.append("and a.FWLB <> ? ");
			list.add(value);
		}
		//签约类别
		value = map.get("QYLB");
		if(!StringUtils.isEmpty(value))
		{
			sbud.append("and a.QYLB = ? ");
			list.add(value);
		}
		//服务状态
		value = map.get("FWZT");
		if(!StringUtils.isEmpty(value))
		{
			sbud.append("and a.FWZT = ? ");
			list.add(value);
		}
		//行政区划代码
		value = map.get("XZQHDM");
		if(!StringUtils.isEmpty(value))
		{
			sbud.append(" and ( ");
			int count = value.length()/2;
			List xzqhSqlList = new ArrayList(count);
			for(int i = 0 ; i < count ;i ++ ){
				String xzqh = value.substring(0 , (i+1)*2);
				xzqhSqlList.add(" (a.XZQHDM = ? and a.JGLB like ?) ");
				list.add(xzqh);
				list.add(jglb+"%");
			}
			sbud.append(String.join(" or ", xzqhSqlList));
			
			value = map.get("YLJGID");
			if(!StringUtils.isEmpty(value))
			{
				sbud.append("or a.YLJGID = ? or  a.XJJGID = ? ");
				list.add(value);
				list.add(value);
			}
			sbud.append(" ) ");
			
		}else if(!StringUtils.isEmpty(value = map.get("YLJGID"))){
			//只查询本机构
			sbud.append("and ( a.YLJGID = ? or  a.XJJGID = ? )");
			list.add(value);
			list.add(value);
		}
		
		result.put("sql", sbud.toString());
		result.put("sqlParams", list);
		
		return result;
	}

	/**
	 * 根据多个ID查询服务项
	 * @param fwidAry
	 * @return
	 */
	/*public JSONArray queryList(String[] fwidAry) {
		StringBuilder sbud = new StringBuilder("select a.FWID,a.FWMC,a.FWNR,a.SFBZ,a.BZ,a.FWZT,a.YLJGID,a.CZRYID,a.FWPC,DATE_FORMAT(a.CJSJ,'%Y-%m-%d %T') CJSJ,b.YLJGMC, \n");
		sbud.append("(select XMMC from TB_ZY_SERVICE_TYPE where XMDM = a.FWLB) FWLBMC , \n");
		sbud.append("(select XMMC from TB_ZY_SFLX where XMDM = a.SFLX) SFLXMC , \n");
		sbud.append("(select XMMC from TB_ZY_QY_TYPE where XMDM = a.QYLB) QYLBMC , \n");
		sbud.append("(select XMMC from TB_ZY_ZT where XMDM = a.FWZT) FWZTMC, \n");
		sbud.append("(select XMMC from TB_ZY_FWPCLX where XMDM = a.FWPCLX) FWPCLXMC \n");
		sbud.append(" from TB_XXWH_FWXX a, TB_ZD_YLJGXX b ");
		sbud.append(" where a.YLJGID = b.PTJGID and a.FWID in (");
		String params = "";
		for (String string : fwidAry) {
			params += "?,";
		}
		params = params.substring(0, params.lastIndexOf(","));
		sbud.append(params);
		sbud.append(")\n");
		SQLBean sb = new SQLBean(sbud.toString(),fwidAry);
		return super.queryList(sb);
	}*/

	@Override
	public JSONArray queryList(Map<String, String> map) {
		StringBuilder sbud = new StringBuilder("select a.*,");
		sbud.append("(select XMMC from TB_ZY_SERVICE_TYPE where XMDM = a.FWLB) FWLBMC,");
		sbud.append("(select XMMC from TB_ZY_ZT where XMDM = a.FWZT) FWZTMC,");
		sbud.append("(select XMMC from TB_ZY_QY_TYPE where XMDM = a.QYLB) QYLBMC  ");
		sbud.append(" from TB_XXWH_FWXX a where a.FWZT = '1' \n");
		List list = new ArrayList();
		String value = map.get("XZQHDM");
		if(!StringUtils.isEmpty(value))
		{
			/*int length = value.length();
			if(length == 6){
				sbud.append("and a.XZQHDM in(?,?,?)");
				list.add(value);
				list.add(value.substring(0, 4));
				list.add(value.substring(0, 2));
			}
			else if(length == 4){
				sbud.append("and a.XZQHDM in(?,?)");
				list.add(value);
				list.add(value.substring(0, 2));
			}
			else if(length == 2){
				sbud.append("and a.XZQHDM = ?");
				list.add(value);
			}*/
			sbud.append(" and ( ");
			int count = value.length()/2;
			List xzqhSqlList = new ArrayList(count);
			for(int i = 0 ; i < count ;i ++ ){
				String xzqh = value.substring(0 , (i+1)*2);
				xzqhSqlList.add(" (a.XZQHDM = ? and a.JGLB like ?) ");
				list.add(xzqh);
				list.add(jglb+"%");
			}
			sbud.append(String.join(" or ", xzqhSqlList));
			
			value = map.get("YLJGID");
			if(!StringUtils.isEmpty(value))
			{
				sbud.append("or a.YLJGID = ? or  a.XJJGID = ? ");
				list.add(value);
				list.add(value);
			}
			sbud.append(" ) ");
		}
		value = map.get("QYLB");
		if(!StringUtils.isEmpty(value))
		{
			sbud.append(" and a.QYLB = ? ");
			list.add(value);
		}
		SQLBean sb = new SQLBean();
		sb.setSql(sbud.toString());
		sb.setParamAry(list);
		return super.queryList(sb);
	}

	/**
	 * 查询服务项概要信息
	 * @return
	 */
	public JSONObject queryPageSummaryFwxx(Map<String, String> map){
		StringBuilder sbud = new StringBuilder("select FWID, FWMC, FWNR from TB_XXWH_FWXX where 1=1 and FWZT='1' \n");
		List list = new ArrayList();
		/*String value = map.get("PTJGID");
		if(!StringUtils.isEmpty(value)){
			sbud.append(" and YLJGID=?");
			list.add(value);
		}*/
		String value = map.get("FWMC");
		if(!StringUtils.isEmpty(value)){
			sbud.append(" and FWMC like ?");
			list.add("%" + value + "%");
		}
		value = map.get("QYLB");
		if(!StringUtils.isEmpty(value)){
			sbud.append(" and QYLB=?");
			list.add(value);
		}
		value = map.get("XZQHDM");
		if(!StringUtils.isEmpty(value))
		{
			sbud.append(" and ( ");
			int count = value.length()/2;
			List xzqhSqlList = new ArrayList(count);
			for(int i = 0 ; i < count ;i ++ ){
				String xzqh = value.substring(0 , (i+1)*2);
				xzqhSqlList.add(" (XZQHDM = ? and JGLB like ?) ");
				list.add(xzqh);
				list.add(jglb+"%");
			}
			sbud.append(String.join(" or ", xzqhSqlList));
			
			value = map.get("PTJGID");
			if(!StringUtils.isEmpty(value))
			{
				sbud.append("or YLJGID = ? ");
				list.add(value);
			}
			sbud.append(" )");
		}
		PageQuerySQLBean sb = super.checkAndCreatePageQuerySQLBean(map, sbud.toString(), "order by CJSJ DESC", list.toArray());
		return super.queryPage(sb, ResultHandlerConstant.JSONARRAY);
	}

	/**
	 * 判断是否存在相同名称
	 * @param map
	 * @return
	 */
	public JSONObject checkSaveName(Map<String, String> map){
		StringBuffer sqlBuf = new StringBuffer("select FWID from ").append(this.tb.getTableName());
		sqlBuf.append(" where FWMC = ? ");
		List<String> sqlParams = new ArrayList<String>();
		sqlParams.add(map.get("FWMC"));
		
		String FWID = map.get("FWID");
		if(!StringUtils.isEmpty(FWID)){
			sqlBuf.append("and FWID <> ?");
			sqlParams.add(FWID);
		}
		SQLBean bean = new SQLBean(sqlBuf.toString(), sqlParams.toArray());
		return super.queryOne(bean);
	}
	
	/**
	 * 查询协议服务项信息
	 * @param map
	 * @return
	 */
	public JSONArray queryXyfwxx(Map<String, String> map){
		StringBuffer sqlBuf = new StringBuffer("select c.FWID,c.FWMC from TB_XXWH_XYFWXX a ");
		sqlBuf.append("left join TB_JGQY_YLT b on a.XYMBID=b.XYID ");
		sqlBuf.append("left join TB_XXWH_FWXX c on a.FWID=c.FWID where 1=1 ");
		
		List<String> sqlParams = new ArrayList<String>();
		//医联体id
		String value = map.get("YLTID");
		if(!StringUtils.isEmpty(value)){
			sqlBuf.append("and b.YLTID = ? ");
			sqlParams.add(value);
		}
		//协议模板id
		value = map.get("XYMBID");
		if(!StringUtils.isEmpty(value)){
			sqlBuf.append("and a.XYMBID = ? ");
			sqlParams.add(value);
		}
		
		SQLBean bean = new SQLBean(sqlBuf.toString(), sqlParams.toArray());
		return super.queryList(bean);
	}

	/**
	 * 医保项目使用次数+1
	 * @param XMDM
	 */
	public void updateYbxm(String XMDM){
		String sql = "update TB_ZY_YBXM set SYCS=SYCS+1 where XMDM = ?";
		SQLBean bean = new SQLBean(sql, XMDM);
		super.update(bean);
	}
	
}
